<template>
  <div>
    <div class="form-container">
      <div class="title-wrap-register">
        <a-row>
          <a-col :span="18" :offset="10">
            <span class="title">注册</span>
          </a-col>
        </a-row>
      </div>
      <CustomForm
        ref="RegisterForm"
        :formRootInstance="formRootInstance"
        :formRules="formRules"
        :initModel="initModel"
        :tempModel="tempModel"
        :submitButtonOptions="submitButtonOptions"
        :curCaptchaPath="curCaptchaPath"
        @refreshAuthCode="getCaptchaPath"
        @submit="handleSubmitForm"
        @handle-radio-change="radioChange"
      >
        <template v-slot:footer>
          <a-row>
            <a-col :span="6" :offset="11">
              <a class="a-tag-init" @click="reset">重置</a>
            </a-col>
          </a-row>
        </template>
      </CustomForm>
      <Loading :loading="loading" />
    </div>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import CustomForm from '@/components/CustomForm/index'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinLogin from '../mixins/index'
import { captchaImage, Register } from '@/api/user'
import Loading from '@/components/Loading/index'
// import { setStorage } from '@/utils/index'
export default {
  mixins: [mixinConfigSetting, mixinLogin],
  components: {
    CustomForm,
    Loading
  },
  props: {
    formConfig: Object
  },
  data() {
    return {

    }
  },
  computed: {
    ...mapGetters('user', [
      'role',
      'account',
      'visitor',
      'token'
    ])
  },
  mounted() {
    // 验证码
    this.getCaptchaImage()
  },
  methods: {
    ...mapMutations('user', [
      'SET_ACCOUNT',
      'SET_ROLE',
      'SET_TOKEN'
    ]),
    radioChange(value) {
      switch (value) {
        case '1':
          this.formRules['enpName'][0].required = false
          break;
        case '2':
          this.formRules['enpName'][0].required = true
          break;
      }
    },
    reset() {  //调用组件的清空方法
      this.$refs.RegisterForm.resetForm()
    },
    login() {
      let path = '/platformPortal'
      this.$router.push({ path: path })
    },
    getCaptchaImage() {
      captchaImage().then((res) => {
        if (res.code === 200) {
          this.curCaptchaPath = `data:image/gif;base64,${res.img}`
          this.uuid = res.uuid
        }
      })
    },
    getCaptchaPath() {
      this.getCaptchaImage()
    },
    async handleSubmitForm(formValue) {
      this.callWithHandlerError(() => {
        const { form } = formValue
        console.log(form, 'form');
        form.uuid = this.uuid
        this.normalizeLoginForm(form)
        if (form.password !== form.affirmPassword) {
          this.$message.error('两次密码不相同')
          return
        }
        Register(form).then((res) => {
          if (res.code === 200) {
            this.$message.success('注册成功!')
            this.cbOperation('login')
          } else {
            this.getCaptchaPath() // 重新获取验证码
            this.$message.warning(res.msg)
            this.loading = false
          }
        })
      })
    },
    normalizeLoginForm() { // 标准化参数
    }
  }
}
</script>